package com.kcsj.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.user.LoginLog;
import com.kcsj.service.user.LoginLogService;
import com.kcsj.util.Condition;
import com.kcsj.util.ControllerHelper;
import com.kcsj.util.MessageHelper;
import com.kcsj.util.Pagination;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 李鑫元
 * 4th Dec. 2020
 */
@Slf4j
@WebServlet(name="LoginLog",urlPatterns="/loginLog.ctl")
public class LoginLogController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String condition = request.getParameter("condition");
        try {
            if(condition!=null){
                JSONObject jsonObject = JSONObject.parseObject(condition);
                List<Condition> conditions =
                        ControllerHelper.getConditions(jsonObject);
                this.responseLoginLogsByUsername(conditions,response);
            }else {
                this.responseLoginLogs(response, null);
            }
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();

        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();

            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    private void responseLoginLogsByUsername(List<Condition> conditionList,
                                             HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找学生
        Collection<LoginLog> allByUsername =
                LoginLogService.getInstance().findAllByUsername(conditionList);
        String loginLogs_json
                = JSON.toJSONString(allByUsername, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(loginLogs_json);
        //响应department_json到前端
        response.getWriter().println(data);
    }

    //按查询条件响应学生对象
    private void responseLoginLogs(HttpServletResponse response, List<Condition> conditionList)
            throws ServletException, IOException, SQLException {
        //获得所有学生
        Collection<LoginLog> loginLogs = LoginLogService.getInstance().findAll(conditionList);
        String loginLogs_json
                = JSON.toJSONString(loginLogs, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(loginLogs_json);
        //响应departments_json到前端
        response.getWriter().println(data);
    }
}

